package tgc.edu.yzy.web.controller;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.entity.EimAdmin;
import tgc.edu.yzy.entity.EimRegister;
import tgc.edu.yzy.repostory.EimRegisterRepository;
import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.RoleService;

@Controller
@RequestMapping(value="/userlogin")
public class UserLoginController {
		@Autowired
		private Custom custom;
		@Autowired
		private RoleService roleservice;
		@Autowired
		private AdminService admin;
		@Autowired
		private EimRegisterRepository userDAO; 
		
		//跳转到管理员注册界面
		@RequestMapping(value="/userlogin")
		private String yanzhengma() {
			return "userlogin/userlogin";
		}
		/***
		 * 获取验证码  （页面已删除）
		 * @return
		 */
		@RequestMapping(value="/yanzhengma")
		private String userlogin() {
			return "userlogin/userlogin";
		}
		/***
		 * 查询输入的验证码
		 * @param usern
		 * @param session
		 * @return
		 */
		@RequestMapping(value="/yanzhenma")
		@ResponseBody
		private Boolean usern(String usern,HttpSession session) {
			String aaa = (String) session.getAttribute("yzm");
			System.out.println("查每次项目初始报错异常！位置：userLoginController:"+usern);
			if(usern==""||usern==null||aaa==""||aaa==null) {
				return false;
			}else {
				if(aaa.equals(usern)) {
					return true;
				}else {
					return false;
				}
			}
			
		}
		/***
		 * 新增
		 * @param request
		 * @param username1
		 * @param password4
		 * @param zhmc
		 * @param name
		 * @param number
		 * @param session
		 * @return
		 */
		@RequestMapping(value="/add")
		private String add(HttpServletRequest request,String username1,String password4,String zhmc,String name,String number,HttpSession session) {
			session.removeAttribute("yzm");
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			EimAdmin a=new EimAdmin();
			a.setName(name);
			a.setNumber(number);
			a.setUsername(username1);
			a.setManaged(0);
			a.setPassword(encoder.encode(password4));
			a.setZhmc(zhmc);
			a.getRoles().add(roleservice.findById(2));
			Date date = new Date();
			Calendar cal = Calendar.getInstance();

			cal.setTime(date);//设置起时间
			cal.add(Calendar.YEAR, 1);
			a.setMindate(date);
			a.setMaxdate(cal.getTime());
			admin.save(a);
			String operation="新增";
			String content=name+"注册了租户管理员账号："+username1;
			custom.recorduuid(request, operation, content,name);
			return "redirect:../logout";
		}
		/***
		 * 查询是否有这个用户
		 * @param session
		 * @param usern
		 * @return
		 */
		@RequestMapping(value="usern")
		@ResponseBody
		private Boolean usern(HttpSession session,String usern) {
			EimRegister usernamess = userDAO.findByUsername(usern);//根据账号查找出用户
		    if(usernamess==null) {
		    	return false;
		    }else {
		    	session.setAttribute("admin", usernamess);
		    	return true;
		    }
		}
		//租户管理员修改密码
		@RequestMapping(value="/userpassword")
		private String userpassword(String qqnumber,String passwor2,HttpServletRequest request,HttpSession session) {
			session.removeAttribute("yzm");
			System.out.println(""+qqnumber+"passwor2"+passwor2);
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			EimRegister id ;
				id=userDAO.findByUsername(qqnumber);
				id.setPassword(encoder.encode(passwor2));
				userDAO.save(id);
				String operation="修改";
				String content=id.getName()+"修改了密码！";
				custom.recorduuid(request, operation, content,id.getName());
			
			return "redirect:../logout";
		}
		
}
